﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using SnmpForm.SnmpWrapper;
using System.Threading;

namespace SnmpForm.APConfig
{
    public partial class ucUpdateSystem : ucAPConfig
    {
        public ucUpdateSystem()
        {
            InitializeComponent();
            //
            updateSystemStatus = new UpdateSystemStatus(UpdateSystemStatusMethod);
        }

        public override void Release()
        {
            base.Release();
            EnableStatusGet(false);
        }

        public void EnableStatusGet(bool bEnable)
        {
            if (bEnable)
            {
                timerStatus.Start();
            }
            else
            {
                timerStatus.Stop();
                StopApUpdateSytemStatusTask();
            }
        }

        public delegate void UpdateSystemStatus(int nApId, string status);
        public UpdateSystemStatus updateSystemStatus;
        public void UpdateSystemStatusMethod(int nApId, string strUpdateStatus)
        {
            labelStatus.Text = strUpdateStatus;
        }

        private Thread objThreadApUpdateSystemStatus = null;
        private GetUpdateSystemStatusTask taskApUpdateSystemStatus = null;

        private void StartApUpdateSystemStatusTask()
        {
            if (objThreadApUpdateSystemStatus == null ||
                objThreadApUpdateSystemStatus.ThreadState == ThreadState.Stopped)
            {
                taskApUpdateSystemStatus =
                    new GetUpdateSystemStatusTask(this, m_ap);
                objThreadApUpdateSystemStatus = new Thread(taskApUpdateSystemStatus.Run);
                objThreadApUpdateSystemStatus.Start();
            }
        }

        private void StopApUpdateSytemStatusTask()
        {
            if (objThreadApUpdateSystemStatus != null)
            {
                if (objThreadApUpdateSystemStatus.ThreadState != ThreadState.Stopped)
                {

                    objThreadApUpdateSystemStatus.Abort();
                    objThreadApUpdateSystemStatus = null;
                }
            }
        }

        private void timerStatus_Tick(object sender, EventArgs e)
        {
            StartApUpdateSystemStatusTask();
        }

        private void buttonUpdate_Click(object sender, EventArgs e)
        {
            byte[] bytesIp = ipAddressControlTftp.GetAddressBytes();
            IPAddress addrTftp = new IPAddress(bytesIp);
            if (addrTftp.Equals(ApSearch.IPAddressExtend.ZeroAddress))
            {
                MessageBox.Show("请输入TFTP服务IP");
                return;
            }
            string strTftpIp = IpStrByByte(bytesIp);
            string strFilename = textBoxFilename.Text;
            if (strFilename.Length == 0)
            {
                MessageBox.Show("请输入固件文件名");
                return;
            }
            string strRequest = strTftpIp + "@" + strFilename;

            try
            {
                m_ap.UpdateSystem(strRequest);
            }
            catch (Lextm.SharpSnmpLib.Messaging.TimeoutException ex)
            {
                Console.WriteLine(ex.Message.ToString());
                MessageBox.Show("超时");
            }
            catch (SocketException ex)
            {
                Console.WriteLine(ex.Message.ToString());
                MessageBox.Show("套接口异常");
            }
            catch (ApSnmpException ex)
            {
                MessageBox.Show(ex.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message.ToString());
                MessageBox.Show("未知异常");
            }
        }

        public override bool Update(bool bView = true)
        {
            if (bView)
            {
            }
            else
            {
            }
            return true;
        }
    }
}
